{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "580df4d1",
   "metadata": {},
   "source": [
    "Using Agentic Framework - Open AI SDK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "efcae4ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "from agents import Agent, Runner, trace, function_tool\n",
    "from openai.types.responses import ResponseTextDeltaEvent\n",
    "from typing import Dict\n",
    "import sendgrid\n",
    "import os\n",
    "from sendgrid.helpers.mail import Mail, Email, To, Content\n",
    "import asyncio\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c415d69e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7dfc4ac0",
   "metadata": {},
   "outputs": [],
   "source": [
    "instructions1 = \"You are a sales agent working for ComplAI, \\\n",
    "a company that provides a SaaS tool for ensuring SOC2 compliance and preparing for audits, powered by AI. \\\n",
    "You write professional, serious cold emails.\"\n",
    "\n",
    "instructions2 = \"You are a humorous, engaging sales agent working for ComplAI, \\\n",
    "a company that provides a SaaS tool for ensuring SOC2 compliance and preparing for audits, powered by AI. \\\n",
    "You write witty, engaging cold emails that are likely to get a response.\"\n",
    "\n",
    "instructions3 = \"You are a busy sales agent working for ComplAI, \\\n",
    "a company that provides a SaaS tool for ensuring SOC2 compliance and preparing for audits, powered by AI. \\\n",
    "You write concise, to the point cold emails.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "81d54c11",
   "metadata": {},
   "outputs": [],
   "source": [
    "#We will define agents and add user system prompts to it - Three agents with different email styles\n",
    "\n",
    "salesAgent1 = Agent(\n",
    "    name = \"Professional Agent\",\n",
    "    instructions = instructions1,\n",
    "    model = \"gpt-4o-mini\"\n",
    ")\n",
    "\n",
    "salesAgent2 = Agent(\n",
    "    name = \"Professional Agent\",\n",
    "    instructions = instructions2,\n",
    "    model = \"gpt-4o-mini\"\n",
    ")\n",
    "\n",
    "\n",
    "salesAgent3 = Agent(\n",
    "    name = \"Professional Agent\",\n",
    "    instructions = instructions3,\n",
    "    model = \"gpt-4o-mini\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c56e04a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#We can wrap it all in a trace\n",
    "\n",
    "from asyncio import gather\n",
    "\n",
    "\n",
    "message = \"Write a cold sales email\"\n",
    "\n",
    "\n",
    "with trace(\"Select the best email\"):\n",
    "    results = await asyncio.gather(\n",
    "        Runner.run(salesAgent1, message),\n",
    "        Runner.run(salesAgent2, message),\n",
    "        Runner.run(salesAgent3, message)\n",
    "    )\n",
    "outputs = [result.final_output for result in results ]\n",
    "\n",
    "for output in outputs:\n",
    "    print(output + \"\\n\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "00c6a766",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Now we define the sales picker to decide which is the best agent in this case\n",
    "\n",
    "SalesPicker = Agent(\n",
    "    name = \"Sales Manager\",\n",
    "    instructions=\"You pick the best cold sales email from the given options. \\\n",
    "Imagine you are a customer and pick the one you are most likely to respond to. \\\n",
    "Do not give an explanation; reply with the selected email only.\",\n",
    "    model=\"gpt-4o-mini\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d9cf87a",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"Write a cold sales email\"\n",
    "\n",
    "with trace(\"Best choice picker\"):\n",
    "\n",
    "    results = await asyncio.gather(\n",
    "        Runner.run(salesAgent1, message),\n",
    "        Runner.run(salesAgent2, message),\n",
    "        Runner.run(salesAgent3, message)\n",
    "    )\n",
    "    \n",
    "    outputs = [result.final_output for result in results]\n",
    "\n",
    "    emails = \"Cold sales emails:\\n\\n\".join(outputs)\n",
    "\n",
    "    best = await Runner.run(SalesPicker, emails)\n",
    "\n",
    "    print(f\"Best sales email:\\n{best.final_output}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff16f2c1",
   "metadata": {},
   "source": [
    "A look at traces on open ai  shows how these agents have been wrapped by traces as seen below;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe509986",
   "metadata": {},
   "source": [
    "![My Image](traceshots.png)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
